#define _CRT_SECURE_NO_WARNINGS 1
//#include <iostream>
//#include <string>
//using namespace std;
#include"string.h"
//class string
//{
//private:
//	char* _str;
//	size_t size;
//	size_t capacity;
//};

void test_string1()
{
	string s1;
	string s2(s1);
	string s3("hello world");

	//string(const string & str, size_t pos, size_t len = npos);
	/*string s4(s3, 6, 5);
	cout << s4 << endl;*/

	//string s5("hello wrold", 20);
	//cout << s5 << endl;

	string s6(10, 'x');
	//cout << s6 << endl;

	//string s7(s5.begin(), s5.begin() + 3);
	//cout << s7 << endl;
}

void test_string2()
{
	//string s1("hello world");

	///*cout << s1 << endl;
	//cin >> s1;
	//cout << s1 << endl;*/

	//string s1("hello world");
	//cout << s1[0] << endl;
	//s1[0] = 'x';
	//cout << s1 << endl;

	string s2("hello world");
	cout << s2.at(0) << endl;

	cout << s2.front() << endl;
	cout << s2.back() << endl;

	//cout << s2[12] << endl;
	cout << s2.at(11) << endl;

	/*string s3;
	s3.back();*/
}

void test_string3()
{
	string s1("hello world");

	size_t i = 0;
	for (; i < s1.size(); i++)
	{
		cout << s1[i] << " ";
	}
	cout << endl;

	string::iterator si = s1.begin();
	while (si != s1.end())
	{
		cout << *si << " ";
		++si;
	}

	for (auto e : s1)
	{
		cout << e << ' ';
	}
}

void test_string4()
{
	string s1("hello world");
	cout << s1 << endl;

	string::iterator si = s1.begin();
	while (si != s1.end())
	{
		*si += 2;
		cout << *si << " ";
		++si;
	}
	cout << endl;

	string::reverse_iterator rsi = s1.rbegin();
	while (rsi != s1.rend())
	{
		cout << *rsi << " ";
		++rsi;
	}
	cout << endl;

	const string s2(s1);
	string::const_iterator csi = s2.begin();
	while (csi != s2.end())
	{
		//*csi += 2;
		cout << *csi << " ";
		++csi;
	}
	cout << endl;
}

//void func1(auto a = 1)
//{}

int func2()
{
	return 1;
}

auto func3()
{
	return func2();
}

auto func4()
{
	return func3();
}

void test_string5()
{
	auto a = 1;//int
	auto b = a;//int
	auto c = 1.1;//double
	auto d = 'x';//char

	cout << typeid(a).name() << endl;
	cout << typeid(b).name() << endl;
	cout << typeid(c).name() << endl;
	cout << typeid(d).name() << endl;

	//auto e;

	//auto arrary[] = { 1,2,3 };

	string s("hello world");
	string::const_reverse_iterator crs1i = s.crbegin();
	auto crsi = s.crbegin();
}

void TestPushBack()
{
	
	string s;
	size_t sz = s.capacity();
	cout << "capacity changed: " << sz << '\n';

	cout << "making s grow:\n";
	for (int i = 0; i < 100; ++i)
	{
		s.push_back('c');
		if (sz != s.capacity())
		{
			sz = s.capacity();
			cout << "capacity changed: " << sz << '\n';
		}
	}
}

void test_string6()
{
	string s("hello world");

	/*cout << s.size() << endl;
	cout << s.length() << endl;*/

	string s1("xxxxxxxxxxxxxxxxxxx");
	//cout << s1.size() << endl;
	//cout << s1.capacity() << endl;
	//TestPushBack();

	cout << sizeof(s) << endl;

}

//class string
//{
//private:
//	char buffer[16];
//	char* _str;
//	size_t _size;
//	size_t _capacity;
//};


void test_string7()
{
	string s("hello worldxxxxxx");
	cout << s.empty() << endl;
	s.clear();
	cout << s.empty() << endl;

	//cout << s << endl;
	//cout << "size:" << s.size() << endl;
	//cout << "capacity" << s.capacity() << endl << endl;

	/*s.reserve(100);
	cout << "size:" << s.size() << endl;
	cout << "capacity" << s.capacity() << endl << endl;

	s.reserve(20);
	cout << "size:" << s.size() << endl;
	cout << "capacity" << s.capacity() << endl << endl;

	s.reserve(10);
	cout << "size:" << s.size() << endl;
	cout << "capacity" << s.capacity() << endl << endl;*/

	/*s.clear();
	cout << "size:" << s.size() << endl;
	cout << "capacity" << s.capacity() << endl << endl;*/
}

void test_string8()
{
	/*string s("hello worldxxxxxx");
	cout << "size:" << s.size() << endl;
	cout << "capacity" << s.capacity() << endl << endl;

	s.reserve(100);
	cout << "size:" << s.size() << endl;
	cout << "capacity" << s.capacity() << endl << endl;

	s.shrink_to_fit();
	cout << "size:" << s.size() << endl;
	cout << "capacity" << s.capacity() << endl << endl;*/

	string s("hello world");
	//cout << s << endl;

	string s1("hello xsc");

	s1.append(s, 6, 100);
	//s1.append(s,6);
	//cout << s1 << endl;

	//s1.append(s);
	//cout << s1 << endl;

	

	/*s1.append("xxxxxxxxxx");
	cout << s1 << endl;

	s1.append("abcdefg", 3);
	cout << s1 << endl;*/

	string s3("hello world");
	cout << s3 << endl;

	s3 += 'x';
	s3 += "abcdefg";
	s3 += s1;
	cout << s3 << endl;


}

void test_string9()
{
	string s("hello world");
	//cout << s << endl;

	//s.erase(0,1);
	s.erase(s.begin());
	//cout << s << endl;

	//s.erase(6);
	/*s.erase(6, 100);
	cout << s << endl;*/


	string s1("hello                       wrold");
	cout << s1 << endl;

	size_t pos = s1.find(' ');
	while (pos != string::npos)
	{
		s1.replace(pos, 1, "%%");
		pos = s1.find(' ', pos + 2);
		cout << s1 << endl;

	}
	cout << s1 << endl;

	string tmp;
	for (auto e : s1)
	{
		if (e == ' ')
		{
			tmp += "%%";
		}
		else
		{
			tmp += e;
		}
	}
	//cout << s1 << endl;

}

void SplitFilename(const std::string& str)
{
	std::cout << "Splitting: " << str << '\n';
	std::size_t found = str.find_last_of("/\\");
	std::cout << " path: " << str.substr(0, found) << '\n';
	std::cout << " file: " << str.substr(found + 1) << '\n';
}

void test_string10()
{
	/*string s("hello.zip.cpp");
	size_t pos = s.rfind('.');
	string ans = s.substr(pos);
	cout << ans << endl;*/

	/*std::string str("Please, replace the vowels in this sentence by asterisks.");
	std::cout << str << '\n';

	std::size_t found = str.find_first_of("abcdef");
	while (found != std::string::npos)
	{
		str[found] = '*';
		found = str.find_first_of("abcdef", found + 1);
	}

	std::cout << str << '\n';*/

	std::string str1("/usr/bin/man");
	std::string str2("c:\\windows\\winhelp.exe");

	SplitFilename(str1);
	SplitFilename(str2);
}

void test_string11()
{
	string s("hello world");
	cout << s.size() << endl;
	cout << s.capacity() << endl << endl;

	s.resize(5);
	cout << s.size() << endl;
	cout << s.capacity() << endl << endl;

	s.resize(14);
	cout << s.size() << endl;
	cout << s.capacity() << endl << endl;

	s.resize(30);
	cout << s.size() << endl;
	cout << s.capacity() << endl << endl;

	/*string s("hello world");
	char buffer[10] = { 0 };

	s.copy(buffer, 3);
	cout << buffer << endl;*/

	//string s;
	//cin >> s;
	//cout << s << endl;

	/*getline(cin, s);
	cout << s;*/
}

//int main()
//{
//	test_string11();
//	//auto a = func4();
//	//cout<<sizeof(string);
//
//	return 0;
//}

void test_mystring1()
{
	xsc::string s1;
	xsc::string s2("hello world");

	//cout << s1.c_str() << endl;
	cout << s2.c_str() << endl;

	xsc::string::iterator si = s2.begin();
	while (si != s2.end())
	{
		cout << *si << " ";

		++si;
	}
	cout << endl;

	for (auto e : s2)
	{
		cout << e << " ";
	}
}

void test_mystring2()
{
	xsc::string s("hello world");
	cout << s.size() << endl;
	cout << s.capacity() << endl << endl;

	s.push_back('x');
	cout << s.c_str() << endl;
	cout << s.size() << endl;
	cout << s.capacity() << endl << endl;

	s.push_back('x');
	s.push_back('x');
	s.push_back('x');
	s.push_back('x');
	s.push_back('x');
	s.push_back('x');
	cout << s.size() << endl;
	cout << s.capacity() << endl << endl;

	s.append("1111111111");
	cout << s.c_str() << endl;
	cout << s.size() << endl;
	cout << s.capacity() << endl << endl;

	s += '0';
	cout << s.c_str() << endl;
	cout << s.size() << endl;
	cout << s.capacity() << endl << endl;

	s += "g++ test_string -o test";
	cout << s.c_str() << endl;
	cout << s.size() << endl;
	cout << s.capacity() << endl << endl;
}

void test_mystring3()
{
	xsc::string s("hello world");
	cout << s.c_str() << endl;

	s.insert(5, 'x');
	cout << s.c_str() << endl;

	s.insert(0, 'x');
	cout << s.c_str() << endl;

	s.insert(s.size(), 'x');
	cout << s.c_str() << endl;


	xsc::string s1("hello world");
	cout << s1.c_str() << endl;

	s1.insert(s1.size(), "abcd");
	cout << s1.c_str() << endl;

}

void test_mystring4()
{
	xsc::string s("hello world");
	cout << s.c_str() << endl;

	s.erase(5);
	cout << s.c_str() << endl;

	xsc::string s1("hello world");
	cout << s1.c_str() << endl;

	/*size_t pos = s1.find('w');
	xsc::string ans = s1.substr(pos);
	cout << ans.c_str() << endl;*/

	/*xsc::string s2;
	s2.reserve(100);*/

	xsc::string s3(s1);

	s3 = s1;
}

void test_mystring5()
{
	xsc::string s("hello world");
	cout << s.size() << endl;
	cout << s.capacity() << endl << endl;

	xsc::string s1(s);
	cout << s1.size() << endl;
	cout << s1.capacity() << endl << endl;

	xsc::string s2("xxxxxxxxxxxxxxxxxxx");

	s1 = s2;

}

int main()
{
	//test_mystring5();

	/*char buff[] = "abc";

	char f[] = "牛马";
	cout << strlen(f) << endl;*/

	std::string s1("hello world");
	std::string s2(s1);

	printf("%p\n", s1.c_str());
	printf("%p\n", s2.c_str());

	return 0;
}